<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  </head>
  <body>
    <button id="btnGet">GET</button>
    <button id="btnPost">POST</button>
    <button id="btnJsonp">JSONP</button>
    <script>
      $(() => {
        // 存在跨域问题,需要加中间件cors
        //  测试get
        $("#btnGet").on("click", function () {
          $.ajax({
            type: "GET",
            url: "http://127.0.0.1:8080/api/get",
            data: { name: "jack", age: 18 },
            success: function (res) {
              console.log(res);
            },
          });
        });
        // 测试post
        $("#btnPost").on("click", () => {
          $.ajax({
            type: "POST",
            url: "http://127.0.0.1:8080/api/post",
            data: { bookname: "水浒传", author: "施耐庵" },
            success: function (res) {
              console.log(res);
            },
          });
        });
        // 测试jsonp
        $("#btnJsonp").on("click", () => {
          $.ajax({
            type: "GET",
            url: "http://127.0.0.1:8080/api/jsonp",
            dataType: "jsonp",
            success: function (res) {
              console.log(res);
            },
          });
        });
      });
    </script>
  </body>
</html>
